package cn.edu.xjtu.android.emoji_warfare_android;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import cn.edu.xjtu.android.emoji_warfare_android.game.DataManager;
import cn.edu.xjtu.android.emoji_warfare_android.game.DataManager.Record;

/**
 * ScoreActivity<br>
 * 高分榜
 * 
 * @author bill
 *
 */
public class ScoreActivity extends Activity {
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
        lv = (ListView) findViewById(R.id.listView);
        // 获取得分情况
        DataManager dm = new DataManager(this);
        Record[] rs = dm.getTops(20);
        ArrayList<Record> data = new ArrayList<Record>();
        for (Record r : rs) {
            data.add(r);
        }
        // 显示数据
        RecordAdapter ra = new RecordAdapter(this, data);
        lv.setAdapter(ra);
    }

    /**
     * 得分情况适配器
     * 
     * @author bill
     *
     */
    public static class RecordAdapter extends ArrayAdapter<Record> {
        public RecordAdapter(Context context, ArrayList<Record> data) {
            super(context, R.layout.item_record, data);
        }

        /**
         * 用于提高效率的ViewHolder
         * 
         * @author bill
         *
         */
        private class ViewHolder {
            TextView tvMode;
            TextView tvSocre;
            TextView tvTime;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh;
            if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(R.layout.item_record, parent, false);

                vh = new ViewHolder();
                vh.tvMode = (TextView) convertView.findViewById(R.id.recordMode);
                vh.tvSocre = (TextView) convertView.findViewById(R.id.recordScore);
                vh.tvTime = (TextView) convertView.findViewById(R.id.recordTime);
                convertView.setTag(vh);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            Record r = getItem(position);
            vh.tvMode.setText(r.mode);
            vh.tvSocre.setText(String.valueOf(r.score));
            vh.tvTime.setText(DateFormat.format("yyyy年MM月dd日 HH:mm:ss", r.time));

            return convertView;
        }
    }
}
